/*
 * @Description: 全宗上下文
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2023-02-26 16:17:19
 */

import React, { createContext, useContext, useEffect, useReducer } from 'react';
import { message } from 'antd';
import { ArchivalCodeReducer } from '../reducers/ArchivalCodeReducer';
import {
  ADD_ARCNO_RULE,
  EDIT_ARCNO_RULE,
  SET_ARCNO_RULE,
  SET_SELECT_AT,
  SET_SELECT_FOND
} from '../constants';
import { addArcno, editArcno, fetchArcnoList } from '@/api/rulesettings/ruleSetting';

const initialState = {
  // 当前选中的项目
  selectUnit: {},
  selectFond: {},
  selectAT: {},

  initRule: {},
  // 选中的表格条目
  selectItems: []
};

const ArchivalCodeContext = createContext();

const ArchivalCodeContextProvider = (props) => {
  const [state, dispatch] = useReducer(ArchivalCodeReducer, initialState);

  const actions = {
    /**
     * @description: 打开新增全宗弹窗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    /**
     * @description: 获取分组字段列表
     */
    getArcnoRule() {
      const query = {
        atCode: state.selectAT.atCode,
        atType: state.selectAT.atType
      };
      fetchArcnoList(query).then((res) => {
        const { data } = res;
        console.log('data', data);
        data && dispatch({ type: SET_ARCNO_RULE, data });
      });
    },

    /**
     * @description: 增加分组字段
     * @param {*}
     */
    addArcnoRule(query) {
      query.atCode = state.selectAT.atCode;
      query.atType = state.selectAT.atType;
      query.level = state.selectAT.atLevel;
      return new Promise((resolve, reject) => {
        addArcno(query)
          .then((res) => {
            message.success('创建成功');
            const { data } = res;
            data.key = data.fieldId;
            dispatch({ type: ADD_ARCNO_RULE, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },

    /**
     * @description: 增加分组字段
     * @param {*}
     */
    editArcnoRule(query) {
      query.atCode = state.selectAT.atCode;
      query.atType = state.selectAT.atType;
      query.level = state.selectAT.atLevel;
      return new Promise((resolve, reject) => {
        editArcno(query)
          .then((res) => {
            message.success('编辑成功');
            const { data } = res;
            data.key = data.fieldId;
            dispatch({ type: EDIT_ARCNO_RULE, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    }
  };

  // watch选中的单位来加载全宗
  useEffect(() => {
    if (JSON.stringify(state.selectAT) !== '{}') {
      actions.getArcnoRule();
    }
  }, [state.selectAT]);

  return (
    <ArchivalCodeContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </ArchivalCodeContext.Provider>
  );
};

export const useArchivalCodeContext = () => {
  return useContext(ArchivalCodeContext);
};

export { ArchivalCodeContext, ArchivalCodeContextProvider };
